package cc.mrbird.event.controller;

import cc.mrbird.assets.domain.AssetMana;
import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.controller.BaseController;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.common.util.TimeUtil;
import cc.mrbird.event.domain.EventInformation;
import cc.mrbird.event.service.EventInformationService;
import cc.mrbird.sendInformation.service.IMailService;
import cc.mrbird.unit.domain.UnitInfo;
import cc.mrbird.unit.service.UnitInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Api(description = "事件信息接口")
@Controller
public class EventInformationController extends BaseController {
    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private EventInformationService eventInformationService;
    @Autowired
    private UnitInfoService unitInfoService;
    @Autowired
    private IMailService iMailService;
    @Log("获取事件信息")
    @RequestMapping(value = "eventInformation",method = RequestMethod.POST)
    /* @RequiresPermissions("eventInformation:list")*/
    public String index() {
        return "eventInformation/eventInformation/eventInformation";
    }
    @Log("获取所有事件信息")
    @ApiOperation(value = "获取所有事件信息" ,  notes="获取所有事件信息")
    @RequestMapping(value = "eventInformation/findAllByEvent",method = RequestMethod.GET)
    @ResponseBody
    public ResponseBo  findAllByEvent(String nextTime){
        try {
            if(StringUtils.isNotEmpty(nextTime)){
                nextTime = TimeUtil.ComputationTime(nextTime);
            }else{
                nextTime = null;
            }
            List<EventInformation> eventInformations = eventInformationService.findAllEvent(nextTime);
            return  ResponseBo.ok(eventInformations);
        } catch (Exception e) {
            return ResponseBo.error("获取不同状态信息失败,请联系管理员");
        }
    }

    @ApiOperation(value = "事件信息添加接口")
    @RequestMapping(value = "eventInformation/add",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo addEventInformation(AssetMana assetMana) {
        try {
            EventInformation eventInformation = new EventInformation();

            //设备ID、发生时间、事件类型、设备类型、所属单位、设备地址、事件状态.摄像头信息
            eventInformation.setAsset_id(assetMana.getAssetMana_id());//设备ID
            eventInformation.setEvent_date(new Date());//发生时间
            eventInformation.setAsset_type(assetMana.getAsset_type());//事件类型
            UnitInfo unitInfo = unitInfoService.findById(assetMana.getUnit_id());
            eventInformation.setUnit(unitInfo.getUnit_name());//所属单位
            eventInformation.setEvent_place(assetMana.getDeployment_location());//事件地址
            eventInformation.setEventStatus(0);//事件状态
            this.eventInformationService.add(eventInformation);

            //iMailService.sendSimpleMail();
            return ResponseBo.ok();
        } catch (Exception e) {
            log.error("添加失败", e);
            return ResponseBo.error("添加失败，请重新添加！");
        }
    }
}
